<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>vue中动画的封装</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root">
    <fade :show="show">
      <div>hello world</div>
    </fade>
    <button @click="handleClick">toggle</button>
  </div>
  
  <script>
    // 将动画都封装在组件中，需要的时候直接调用即可
    Vue.component('fade', {
      props: ['show'],
      template: 
           `<transition @before-enter="handleBeforeEnter" @enter="handleEnter">
              <slot v-if="show"></slot>
            </transition>`,
      methods: {
        handleBeforeEnter: function(el) {
          el.style.color = 'red';
        },
        handleEnter: function(el, done) {
          setTimeout(() => {
            el.style.color = "blue";
            done();
          }, 2000)
        }
      }
    })

    var vm = new Vue({
      el: '#root',
      data: {
        show: false
      },
      methods: {
        handleClick: function() {
          this.show = !this.show
        }
      }
    })
  </script>

</body>
</html>